"""
Case Type   : GUC
Case Name   : 使用gs_guc set方法设置参数post_auth_delay为100,观察预期结果
Create At   : 2020/12/2
Owner       : zhuanghuan96
Description :
        1.查询post_auth_delay默认值
        2.修改参数值为100并重启数据库
        3.恢复参数默认值
Expect      :
        1.显示默认值为0
        2.设置成功,小数100s
        3.默认值恢复成功
History     :
    modified: 2023-05-12 by zou_jialiang050 优化恢复环境步骤
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
commonsh = CommonSH('dbuser')


class DeveloperOptions(unittest.TestCase):
    def setUp(self):
        LOG.info(
            '----Opengauss_Function_DeveloperOptions_Case0030start------')
        self.constant = Constant()

    def test_post_auth_delay(self):
        LOG.info('--步骤1:查看默认值--')
        sql_cmd = commonsh.execut_db_sql('show post_auth_delay;')
        LOG.info(sql_cmd)
        self.assertEqual('0', sql_cmd.split('\n')[2].strip())
        LOG.info('--步骤2:设置参数为100并重启数据库--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'post_auth_delay = 100')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info('--步骤3:查询该参数修改后的值--')
        sql_cmd = commonsh.execut_db_sql('show post_auth_delay;')
        LOG.info(sql_cmd)
        self.assertIn('100s', sql_cmd)

    def tearDown(self):
        LOG.info('--步骤4:恢复默认值--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'post_auth_delay=0')
        LOG.info(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status in status)
        LOG.info(
            '------Opengauss_Function_DeveloperOptions_Case0030finish----')
